package com.ruoyi.web.controller.lh;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.lh.domain.Software;
import com.ruoyi.lh.service.ISoftwareService;
import com.ruoyi.lh.service.IUserService;
import com.ruoyi.web.controller.dto.CheckUpdateParams;
import com.ruoyi.web.controller.dto.LoginParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.net.URLEncoder;

/**
 * 连接controller
 */
@RestController
@RequestMapping("/lh/connect")
public class ConnectController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ISoftwareService softwareService;

    @RequestMapping("login")
    public AjaxResult login(@RequestBody @Valid LoginParams loginParams) {
        return userService.login(loginParams);
    }

    @RequestMapping("logout")
    public AjaxResult logout(@RequestBody String logout) {
        return userService.logout(logout);
    }

    @RequestMapping("checkUpdate")
    public AjaxResult checkUpdate(@RequestBody CheckUpdateParams checkUpdateParams) {
        return userService.checkUpdate(checkUpdateParams);
    }

    @RequestMapping("download")
    public void checkUpdate(@RequestBody String downloadParams, HttpServletResponse response) {

        JSONObject jsonObject = JSON.parseObject(downloadParams);
        Long id = jsonObject.getLong("m_ucFileNO");

        Software software = softwareService.selectSoftwareById(id);
        String updateFile = software.getUpdateFile();

        try
        {
            if (!FileUtils.checkAllowDownload(updateFile))
            {
                throw new Exception(StringUtils.format("资源文件({})非法，不允许下载。 ", updateFile));
            }
            // 本地资源路径
            String localPath = RuoYiConfig.getProfile();
            // 数据库资源地址
            String downloadPath = localPath + StringUtils.substringAfter(updateFile, Constants.RESOURCE_PREFIX);
            // 下载名称
            String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, downloadName);
            FileUtils.writeBytes(downloadPath, response.getOutputStream());
        }
        catch (Exception e)
        {
            System.out.println("下载文件失败");
        }
        software.setDownloadNum(software.getDownloadNum() + 1);
        softwareService.updateSoftware(software);

    }

}
